<template>
  <div style="margin-left:15px;margin-top:15px">
      <el-form :model="reserveForm" :inline="true">
        <el-form-item label="预约状态：">
        <el-select
          v-model="reserveForm.status"
          placeholder="请选择预约状态"
          clearable
        >
          <el-option label="已预约" value="0"> </el-option>
          <el-option label="已取消" value="1"> </el-option>
          <el-option label="已开始" value="2"> </el-option>
          <el-option label="已结束" value="3"> </el-option>
          <el-option label="已超时" value="4"> </el-option>
        </el-select>
      </el-form-item>
        <el-form-item v-if="userInfo.type==='1'" label="预约人：">
          <el-input
            v-model="reserveForm.userId"
            placeholder="请输入"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
        <el-button @click="getReserve()">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="reserveData"
        border
        class="table"
        highlight-current-row>
         <el-table-column
          prop="accountNumber"
          label="预约人账号"
          min-width="100"
        >
         </el-table-column>
        <el-table-column
          prop="userName"
          label="预约人姓名"
          min-width="100"
        >
        </el-table-column>
        <el-table-column
          prop="status"
          label="预约状态"
          min-width="100"
        >
        <template slot-scope="scope">
            <span v-if="scope.row.status=='0'">已预约</span>
            <span v-if="scope.row.status=='1'">已取消</span>
            <span v-if="scope.row.status=='2'">已开始</span>
            <span v-if="scope.row.status=='3'">已结束</span>
            <span v-if="scope.row.status=='4'">已超时</span>
        </template>
        </el-table-column>
        <el-table-column
          prop="isPunchClock"
          label="是否打卡"
          min-width="100"
        >
        <template slot-scope="scope">
            <span v-if="scope.row.isPunchClock==='0'">否</span>
            <span v-if="scope.row.isPunchClock==='1'">是</span>
        </template>
        </el-table-column>
        <el-table-column
          prop="punchClockDate"
          label="打卡时间"
          min-width="100"
        >
        </el-table-column>
        <el-table-column
          prop="reserveStartDate"
          label="预约开始时间"
          min-width="100"
        >
        </el-table-column>
        <el-table-column
          prop="reserveEndDate"
          label="预约结束时间"
          min-width="100"
        >
        </el-table-column>
        <el-table-column
          prop="floor"
          label="楼层"
          min-width="100"
        >
        </el-table-column>
        <el-table-column
          prop="roomName"
          label="阅览室名称"
          min-width="100"
        >
        </el-table-column>
        <el-table-column
        v-if="userInfo.type==='0'"
          label="操作"
          min-width="150"
          align="center"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-popconfirm
            v-if="scope.row.status==='0'"
              confirmButtonText="确定"
              cancelButtonText="取消"
              icon="el-icon-info"
              iconColor="red"
              @onConfirm="deleteReserve(scope.row.id)"
              title="确定要取消吗？"
            >
              <el-button slot="reference" type="text" icon="el-icon-delete"
                >取消预约</el-button
              >
            </el-popconfirm>
            <el-button v-if="scope.row.status==='0' || scope.row.status==='2'" @click="openDialog(scope.row)" slot="reference" type="text" icon="el-icon-delete"
                >续约</el-button
              >
          </template>
        </el-table-column>
    </el-table>
    <el-dialog title="续约" :visible.sync="dialogVisible"  width="40%">
        <el-form :model="continueForm">
             <el-form-item label="预约结束时间：">
          <el-date-picker
      v-model="continueForm.reserveEndDate"
      type="datetime"
      value-format="yyyy-MM-dd HH:mm:ss"
      placeholder="选择日期时间">
    </el-date-picker>
        </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="continueReserve"
          >提 交</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import webservice from "@/utils/webservice";
export default {
    data(){
        return{
            reserveForm:{},
            reserveData:[],
            userInfo:{},
            continueForm:{},//续约form
            dialogVisible:false,
        }
    },
    mounted(){
        this.userInfo = JSON.parse(sessionStorage.getItem('userInfo'))
        this.getReserve()
    },
    methods:{
        //预约记录
    getReserve() {
      if(this.userInfo.type==='0'){
        var userId = this.userInfo.id
      }else{
        var userId = this.reserveForm.userId
      }
      const _param = {
        userId: userId ,
        status: this.reserveForm.status,
      };
      const _params = JSON.stringify(_param);
      webservice.HttpPost("/api/reserveRecord/selectReserveRecordList", _params, (data) => {
        if (data.code === 200) {
          this.reserveData = data.data;
        } else {
          this.$message.error({
            message: data.message,
            showClose: true,
            duration: 1000,
          });
        }
      });
    },
    //取消预约
    deleteReserve(id){
            const _param = {
        id: id,
        userId: this.userInfo.id,
      };
      const _params = JSON.stringify(_param);
      webservice.HttpPost("/api/reserveRecord/cancelReserveSeat", _params, (data) => {
        if (data.code === 200) {
            this.$message.success({
            message: '取消成功！',
            showClose: true,
            duration: 1000,
          });
          this.getReserve()
        } else {
          this.$message.error({
            message: data.message,
            showClose: true,
            duration: 1000,
          });
        }
      });
    },
    //续约弹框
    openDialog(row){
      this.continueForm = JSON.parse(JSON.stringify(row));
      this.dialogVisible = true;
    },
    continueReserve(){
         const _param = {
        id: this.continueForm.id,
        reserveEndDate: this.continueForm.reserveEndDate,
      };
      const _params = JSON.stringify(_param);
      webservice.HttpPost("/api/reserveRecord/addReserveTime", _params, (data) => {
        if (data.code === 200) {
            this.$message.success({
            message: '续约成功！',
            showClose: true,
            duration: 1000,
          });
          this.dialogVisible = false;
          this.getReserve()
        } else {
          this.$message.error({
            message: data.message,
            showClose: true,
            duration: 5000,
          });
        }
      });
    },
    }
}
</script>

<style>

</style>